// pages/product/curseinfo/curseinfo.js
let AJAX = require("../../../utils/util.js").AJAX
let timeTranfer = require("../../../utils/util.js").timeTranfer
const App = getApp()
let productKind = require('../../../utils/util.js').productKind
let formatRichText = require('../../../utils/util.js').formatRichText
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH:"",
    curseid:'',
    dataInfo:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: App.globalData.navHeight,
      curseid: options.id || ''
    })
    this.requestInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navBack(){
    wx.navigateBack()
  },
  requestInfo() {
    let vm = this
    AJAX("product/info", "GET", {
      id: vm.data.curseid
    }).then(res => {
      let item = res.data
      item.product_kind = productKind[item.product_kind || 1]
      item.theme_name = item.theme_name

      if (item.theme_name) {
        item.theme_name = item.theme_name.split('|')
      }
      
      // 费用包含 dataInfo.productDetails.cost_list
      item.productDetails.cost_list = formatRichText(item.productDetails.cost_list).replace(/<section/g, '<div').replace(/\/section>/g, '\div>')
      // item.productDetails.about_us = formatRichText(item.productDetails.about_us).replace(/<section/g, '<div').replace(/\/section>/g, '\div>')
      item.productDetails.notice = formatRichText(item.productDetails.notice).replace(/<section/g, '<div').replace(/\/section>/g, '\div>')
      item.productDetails.content = formatRichText(item.productDetails.content).replace(/<section/g, '<div').replace(/\/section>/g, '\div>')
      if (item.productPoint) {
        item.productPoint.forEach(function (it, index) {
          item.productPoint[index].point_content = formatRichText(it.point_content).replace(/<section/g, '<div')
            .replace(/\/section>/g, '\div>');
        })
      }
      if (item.begin_time) {
        item.begin_time = timeTranfer(item.begin_time, 1)
      }
      if (item.end_time) {
        item.end_time = timeTranfer(item.end_time, 1)
      }

      vm.setData({
        dataInfo: item
      })
    })
  },
  colTap(e) {
    let vm = this
    let _val = e.currentTarget.dataset.col == 1 ? 0 : 1
    AJAX("member-collection/execute", "POST", {
      product_id: vm.data.proid,
      execute_type: _val
    }).then(res => {
      wx.showToast({
        title: (_val ? "收藏成功" : "取消收藏成功"),
        icon: "none",
        duration: 3000,
        success:function(){
          vm.requestInfo()
        }
      })
      
    })
  },
  makePhone(e) {
    let _phone = e.currentTarget.dataset.phone
    if(!_phone){
      return
    }
    wx.makePhoneCall({
      phoneNumber: _phone,
    })
  },
  writeOrder(e) {
    let _id = e.currentTarget.dataset.id
    if (this.data.dataInfo.schedule_type == 1) {
      // 成品
      wx.navigateTo({
        url: '/pages/order/orderwrite/orderwrite?id=' + _id,
      })
    } else {
      // 预售
      wx.navigateTo({
        url: '/pages/order/reserveorder/reserveorder?id=' + _id,
      })
    }

  },
  codeRead(e) {
    let _url = e.currentTarget.dataset.url
    if(!_url){
      return
    }
    wx.previewImage({
      urls: [_url],
    })
    // wx.getImageInfo({// 获取图片信息（此处可不要）
    //   src: _url,
    //   success: function (res) {
    //     debugger
    //   }
    // })
  }
})